import { TODO_ADD, TODO_DEL_BY_ID, TODO_UPDATE } from "../action/actionType";

const initialState = {
  list: [
    {
      id: 1,
      task: "React",
      isDone: false,
    },
    {
      id: 2,
      task: "React2",
      isDone: true,
    },
    {
      id: 3,
      task: "React3",
      isDone: false,
    },
  ],
  type: "all",
};

export default function todoReducer(state = initialState, { type, payload }) {
  switch (type) {
    case TODO_UPDATE:
      return {
        list: state.list.map((item) => {
          return {
            ...item,
            isDone: payload === item.id ? !item.isDone : item.isDone,
          };
        }),
      };
    case TODO_DEL_BY_ID:
      console.log("123");
      return {
        list: state.list.filter((item) => item.id !== payload),
      };
    case TODO_ADD:
      // console.log("123");
      return {
        list: [...state.list, payload],
      };

    default:
      return state;
  }
}
